// pages/member/bind-bankcard00.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    issend:false,//验证码按钮
    real_name: '',
    bank_card: '',
    phone: '',
    code: '',
    remsg: '',
    userinfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let userinfo = app.http.getUserInfo()
      this.setData({
        userinfo: userinfo,
        real_name: userinfo.real_name
      })

  },
  sendCode() {
    this.sendmsg();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  bindChangePhoneNumber(event) {
    this.data.phone = event.detail.value;
  },

  bindChangeCardNumber(event) {
    this.data.bank_card = event.detail.value;
  },
  bindChangeCodeNumber(event) {
    this.data.code = event.detail.value;
  },
  
  sendmsg: function () {
    var that = this;
    var phone = that.data.phone;
    if (!app.util.isPhone(phone)) {
      return wx.showToast({
        title: '请输入正确的手机号码',
        duration: 2000,
        icon: 'none'
      })
    }
   
    //发送短信
    var time = 60;
    var inter = null;
    app.http.$ajax(
      {
        url: 'sms/send',
        data: { 'mobile': that.data.phone, event: 'renzheng' }
      },
      {
        success(data) {
          clearInterval(inter)
          that.setData({ issend: true })
          inter = setInterval(function () {
            that.setData({
              'remsg': "重新发送(" + time + "s)",
            })
            time--
            if (time < 0) {
              clearInterval(inter)
              that.setData({
                'remsg': "获取验证码",
              });
              that.setData({ 'issend': false });
            }
          }, 1000);
          
        }
      },
      {
        loading: '发送中...'
      }
    )
  },
  submit() {
    var that = this;
    //对数据进行认证
    if (!this.data.real_name || app.util.strlen(this.data.real_name) < 2) {
      return wx.showToast({
        title: '请正确输入姓名',
        duration: 2000,
        icon: 'none'
      })
    }

    if (!this.data.bank_card ||app.util.strlen(this.data.bank_card) < 14) {
      return wx.showToast({
        title: '请正确输入银行卡号',
        duration: 2000,
        icon: 'none'
      })
    }
    if (!app.util.isPhone(this.data.phone)) {
      return wx.showToast({
        title: '请正确输入手机号',
        duration: 2000,
        icon: 'none'
      })
    } 
    if (app.util.strlen(this.data.code) != 4) {
      return wx.showToast({
        title: '验证码为4位',
        duration: 2000,
        icon: 'none'
      })
    }

    app.http.$ajax(
      {
        url: 'v1.user/saveBank',
        data: { 
          real_name: that.data.real_name,
          bank_card: that.data.bank_card,
          mobile: that.data.phone, 
          code: that.data.code,
        },
        loading: '银行卡认证中...'
      },
      {
        success(data) {
          app.http.saveUser( data.data)

          wx.showToast({
            title: '绑定成功',
            duration: 2000,
            icon: 'none',
          })

          setTimeout(function() {
             wx.reLaunch({
               url: '/pages/tab/index',
             })
          }, 1000);
        }
      }
    )

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})